package com.alpsot.spotcheck.activity;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.EditText;

import com.alpsoft.spotcheck.impl.HandleMsg;
import com.alpsoft.spotcheck.util.DialogUtil;

import cn.pda.scan.ScanThread;

/**
 * 父窗体集成弹出对话框和后台运行功能。
 * 
 * @author Nasir
 * 
 */
public abstract class BaseActivity extends FragmentActivity implements HandleMsg{
	private boolean isActive = true;
	private PdaScanReceiver receiver;

	public DialogUtil mDialogUtil = new DialogUtil(this);
	public Dialog mBaseDialog = null;

	public void showLoadingDialog(String msg) {
		if (msg != null) {
			if (mBaseDialog != null && mBaseDialog.isShowing()) {
				mBaseDialog.dismiss();
				mBaseDialog = null;
			}
			mBaseDialog = mDialogUtil.showLoading(msg);
		}
	}

	public void hideLoadingDialog() {
		if (mBaseDialog != null && mBaseDialog.isShowing()) {
			mBaseDialog.dismiss();
		}
	}

	public boolean isAppOnForeground() {
		// 判断是否后台运行
		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;

		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}
		return false;
	}


	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		receiver = new PdaScanReceiver();
		// 代码注册功能键广播接收者
		IntentFilter filter2 = new IntentFilter();// 定义过滤器
		filter2.addAction(PdaScanReceiver.ACTION_SCAN_3505);// 添加监听事件:监听功能键
		registerReceiver(receiver, filter2);// 将上下文环境(容器)定义为事件结果的接收者.
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	public class PdaScanReceiver extends BroadcastReceiver {

		public static final String ACTION_SCAN_3505 = "com.zkc.scancode";

		@Override
		public void onReceive(Context context, Intent intent) {

			// 鲁抗防爆平台3505的广播接收测试
			if (ACTION_SCAN_3505.equals(intent.getAction())) {
				String code = intent.getStringExtra("code");
				Message msg = new Message();
				View view = getCurrentFocus();
				if (view instanceof EditText) {
					((EditText) view).setText(code);
					((EditText) view).selectAll();
				}
				msg.what = ScanThread.SCAN;
				Bundle bundle = new Bundle();
				bundle.putString("data", code);
				msg.setData(bundle);
				if (getHandler() == null) {
					return;
				}
				getHandler().removeMessages(ScanThread.SCAN);
				getHandler().handleMessage(msg);
				return;
			}
		}
	}
}
